/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.devmng.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.devmng.dao.DeviceMainDao;
import com.thinkgem.jeesite.modules.devmng.entity.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备Service
 *
 * @author panda
 * @version 2017-12-02
 */
@Service
@Transactional(readOnly = true)
public class DeviceMainService extends CrudService<DeviceMainDao, DeviceMain> {

    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private DeviceManualService deviceManualService;


    @Value("${web.upload.path}")
    private String webUploadPath;

    @Value("${web.upload.url.prefix}")
    private String webUploadUrlPrefix;

    @Autowired
    private CommonAttaService commonAttaService;

    public List<DeviceMain> findListByTaskMainId(String taskMainId) {
        return this.dao.findListByTaskMainId(taskMainId);
    }

    @Transactional(readOnly = false)
    public List<DeviceMain> findListIdBySysOffice(String officeId) {
        return dao.findListIdBySysOffice(officeId);
    }
//    public List<DeviceMainWithStatus> findListWithStatusByTaskMainId(String taskMainId) {
//        return this.dao.findListWithStatusByTaskMainId(taskMainId);
//    }

    public DeviceMain get(String id) {
        DeviceMain deviceMain = super.get(id);
        if (deviceMain != null && deviceMain.getDeviceType() != null && deviceMain.getDeviceType().getId() != null) {
            DeviceType deviceType = deviceTypeService.get(deviceMain.getDeviceType().getId());
            deviceMain.setDeviceType(deviceType);
        }
        return deviceMain;
    }

    public List<DeviceMain> findList(DeviceMain deviceMain) {
        return super.findList(deviceMain);
    }

    public Page<DeviceMain> findPage(Page<DeviceMain> page, DeviceMain deviceMain) {
        return super.findPage(page, deviceMain);
    }

    @Transactional(readOnly = false)
    public void save(DeviceMain deviceMain, MultipartFile deviceImgFile, MultipartFile[] deviceManualFile) {
        if (!deviceImgFile.isEmpty()) {
            String extension = FilenameUtils.getExtension(deviceImgFile.getOriginalFilename());
            String saveName = IdGen.uuid();
            if (StringUtils.isNotEmpty(extension)) {
                saveName = saveName + "." + extension;
            }
            try {
                deviceImgFile.transferTo(new File(webUploadPath + saveName));
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
            CommonAtta commonAtta = new CommonAtta();
            commonAtta.setAttaGuid(saveName);
            commonAtta.setAttaContentType(deviceImgFile.getContentType());
            commonAtta.setAttaOriname(deviceImgFile.getOriginalFilename());
            commonAtta.setAttaSize(deviceImgFile.getSize());
            commonAtta.setAttaPath(webUploadPath);
            String attaUrl = webUploadUrlPrefix + saveName;
            deviceMain.setDeviceImgUrl(attaUrl);
            commonAtta.setAttaUrl(attaUrl);
            commonAttaService.save(commonAtta);
        }

        MultipartHttpServletRequest request;
        //ArrayList<String> attaUrl = new ArrayList<String>();
        ArrayList<String> fileNameList = new ArrayList<String>();
        String fileUrl ="";
            for (MultipartFile file : deviceManualFile) {
                if (!file.isEmpty()) {
                    String extension = FilenameUtils.getExtension(file.getOriginalFilename());
                    String saveName = IdGen.uuid();
                    if (StringUtils.isNotEmpty(extension)) {
                        saveName = saveName + "." + extension;
                    }
                    try {
                    file.transferTo(new File(webUploadPath + saveName));
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
                CommonAtta commonAtta = new CommonAtta();
                DeviceManual deviceManual = new DeviceManual();
                commonAtta.setAttaGuid(saveName);
                commonAtta.setAttaContentType(file.getContentType());
                commonAtta.setAttaOriname(file.getOriginalFilename());
                commonAtta.setAttaSize(file.getSize());
                commonAtta.setAttaPath(webUploadPath);
                String attaUrl = webUploadUrlPrefix + saveName;
                commonAtta.setAttaUrl(attaUrl);
                deviceMain.setDeviceManualUrl(attaUrl);
                fileUrl = attaUrl;
                deviceManual.setDeviceManualUrl(attaUrl);
                deviceManual.setDeviceManualName(deviceMain.getDeviceName() + "作业指导书");
                deviceManual.setDeviceMain(deviceMain);
                fileNameList.add(file.getOriginalFilename());
                deviceManualService.save(deviceManual);
                commonAttaService.save(commonAtta);
            }
        }
        if (fileUrl != null && fileUrl.length() > 0) {
            deviceMain.setDeviceManualUrl(fileUrl);
            deviceMain.setDeviceManualName(deviceMain.getDeviceName() + "作业指导书");
        }
        deviceMain.setExamine("未审核");
        super.save(deviceMain);
    }

    @Transactional(readOnly = false)
    public void superSave(DeviceMain deviceMain) {
        super.save(deviceMain);
    }


    @Transactional(readOnly = false)
    public void delete(DeviceMain deviceMain) {
        super.delete(deviceMain);
    }

}